import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:seedfour/app/models/case_detail_entity.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_case/case_detail/state.dart';
import 'package:seedfour/toolkit/typedef/function.dart';

import '../../../../constants.dart';

class PaidOrderListWidget extends StatelessWidget {
  final CaseDetailState data;
  final ParamVoidCallback onSelectOrder;

  const PaidOrderListWidget({
    Key? key,
    required this.data,
    required this.onSelectOrder,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Container(
        margin: EdgeInsets.symmetric(horizontal: kSpaceSmall),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(kRadiusMedium),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: kSpaceMedium),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
              child: Text(
                '关联付款订单',
                style: TextStyle(
                  fontSize: kFontSizeMedium,
                  color: AppColor.colorFontBlack,
                  fontWeight: AppFont.fontMedium,
                ),
              ),
            ),
            SizedBox(height: kSpaceMedium),
            const Divider(),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
              child: _buildOrders(data.paidOrderList),
            ),
            if (data.caseStatus.value == 0) ...[
              const Divider(),
              SizedBox(
                width: double.maxFinite,
                child: FilledButton(
                  onPressed: onSelectOrder,
                  style: ElevatedButton.styleFrom(
                    foregroundColor: AppColor.colorFontBlue,
                    backgroundColor: Colors.transparent,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(kRadiusSmall),
                    ),
                    padding: EdgeInsets.zero,
                  ),
                  child: const Text('勾选付款订单'),
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }

  Widget _buildItemDetail(String title, String content,
      {Color? contentColor = AppColor.colorFontBlack}) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: kSpaceMini),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            title,
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: AppColor.colorFontBlack,
            ),
          ),
          Text(
            content,
            style: TextStyle(
              fontSize: kFontSizeSmall,
              color: contentColor,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildOrderItem(CaseOrder order) {
    return Column(
      children: [
        _buildItemDetail('订单号', order.id.toString()),
        _buildItemDetail('创建时间', order.createdAtStr ?? ''),
        _buildItemDetail(
            '付款户名', order.title == null ? '' : order.title!.split(',')[0]),
        _buildItemDetail(
            '付款账号', order.title == null ? '' : order.title!.split(',')[1]),
        _buildItemDetail(
            '开户行', order.title == null ? '' : order.title!.split(',')[2]),
        _buildItemDetail('付款金额', order.orderAmountStr ?? ''),
        _buildItemDetail(
          '到账状态',
          order.orderChargeStatus! < 7 ? '未到账' : '已到账',
          contentColor: order.orderChargeStatus! < 7
              ? AppColor.colorFontRed
              : AppColor.colorFontGreen,
        ),
        _buildItemDetail('到账时间', order.collectionDateStr ?? ''),
      ],
    );
  }

  Widget _buildOrders(List<CaseOrder> orders) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: List.generate(orders.length, (index) {
        if (index == orders.length - 1) {
          return _buildOrderItem(orders[index]);
        } else {
          return Column(
            children: [
              _buildOrderItem(orders[index]),
              SizedBox(height: kSpaceSmall),
              const Divider(),
              SizedBox(height: kSpaceSmall),
            ],
          );
        }
      }),
    );
  }
}
